<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/15
 * Time: 14:53
 */

namespace app\mobile\controller;


use app\Model\GoodsModel;
use think\Controller;
use think\Db;
class Goods extends Controller
{
    //商品搜索
    public function Search(){


        if (empty(input('sort'))){
            $sort = 'goods_id';
        }else{
            $sort = input('sort');
        }
        if (empty(input('sort_asc'))){
            $sort_asc = 'asc';
        }else if(input('sort_asc') == 'asc'){
            $sort_asc = 'desc';
        }else if(input('sort_asc') == 'desc'){
            $sort_asc ='asc';
        }
        $q =trim(input('q'));
//        $filter_goods = GoodsModel::field('*')
//            ->where('is_on_sale',1 )
//            ->where('goods_name','like',"%".$q."%")
//            ->order('sort')
//            ->getField('good_id');
        $filter_goods = Db::name('tp_goods')
            ->where('is_on_sale',1 )
            ->where('keywords|goods_name','like',"%".$q."%")
            ->order($sort,$sort_asc)
            ->page(1,4)
            ->select();
        $this->assign('q',$q);
        $this->assign('sort',$sort);
        $this->assign('sort_asc',$sort_asc);
        $this->assign('filter_goods',$filter_goods);
        $Search = Db::name('tp_config')->select();//热门搜索
        foreach ($Search as $k=>$v) {
            if ($v['name'] == 'hot_keywords') {
                $val = explode('|', $v['value']);
                $this->assign('Search', $val);
            }
        }
        return view();
    }

    /**
     * 商品搜索页面加载更多功能
     */
    public function Ajaxload(){
        $q = input('q');
        $p = input('p');
        if ($p === 'undefined'){
            $p =2;
        }
        if (empty(input('sort'))){
            $sort = 'goods_id';
        }else{
            $sort = input('sort');
        }
       $sort_asc =input('sort_asc');
        $filter_goods = Db::name('tp_goods')
            ->where('is_on_sale',1 )
            ->where('keywords','like',"%".$q."%")
            ->order($sort,$sort_asc)
            ->page($p,4)
            ->select();

        $this->assign('q',$q);
        $this->assign('sort',$sort);
        $this->assign('sort_asc',$sort_asc);
        $this->assign('filter_goods',$filter_goods);
        if ($filter_goods){
            $num = array();
            foreach ($filter_goods as $k=>$v){
                $data = '<li><a onclick="goodsinfo('.$v['goods_id'].')" class="item">';
                $data.='<div class="pic_box"><div class="active_box">';
                $data.='<span style="background-position:0px -36px">新品</span></div>';
                $data.='<img src="'.goods_thum_images($v['goods_id'],400,400).'"></div>';
                $data.='<div class="title_box">'.$v['goods_name'].'</div>';
                $data.='<div class="price_box">';
                $data.='<span class="new_price"><i>￥'.$v['shop_price'].'元</i></span></div>';
                $data.='<div class="comment_box">已售0</div>';
                $data.='<span class="bug_car" onClick="AjaxAddCart()"><i class="icon-shop_cart"></i></span></a></li>';
                $num[] = $data;
            }
        }else{
            $num = null;
        }

        return $num;
    }

}